/**
 */

package com.bei_en.common.util.OSS;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.AppendObjectResult;
import com.bei_en.common.util.GenerateUtil;

/**
 * @author Guocg
 *
 */
public class UploadUtil {

	protected static Logger log = Logger.getLogger(UploadUtil.class);

	public static String endpoint = "oss-cn-hangzhou.aliyuncs.com";
	public static String img_endpoint = "oss-cn-hangzhou.aliyuncs.com";// 图片服务
	public static String accessKeyId = "LTAIxNnI1liVrDRo";
	public static String accessKeySecret = "LeqmPHUONzuWh8vvObrTB3n6jf4xya";

	public static String bucketName = "jiaoshi";

	private static String key;
	private static OSSClient client;

	public static String upload(MultipartFile file) throws Exception {

		client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		String ext = file.getOriginalFilename();
		ext = ext.substring(ext.lastIndexOf("."), ext.length());
		log.warn(ext);
		try {
			if (ext.isEmpty())
				throw new RuntimeException("文件类型未知");
			key = GenerateUtil.geneDate() + "/" + GenerateUtil.geneCode() + ext;

			Long firstPosition = 0L;
			InputStream instream = new ByteArrayInputStream(file.getBytes());
			AppendObjectResult appendObjectResult = client
					.appendObject(new AppendObjectRequest(bucketName, key,
							instream).withPosition(0L));
			// URL url = client.generatePresignedUrl(bucketName, key,
			// DateUtils.addYears(new Date(), 2));
			String url = "http://" + bucketName + "." + img_endpoint + "/"
					+ key;
//			if (!new SexSample().detectImages(url))// 鉴黄
				return key;
//			else
//				return "sexy";
		} catch (OSSException oe) {
			log.warn("Caught an OSSException, which means your request made it to OSS, "
					+ "but was rejected with an error response for some reason.");
			log.warn("Error Message: " + oe.getErrorCode());
			log.warn("Error Code:       " + oe.getErrorCode());
			log.warn("Request ID:      " + oe.getRequestId());
			log.warn("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			log.warn("Caught an ClientException, which means the client encountered "
					+ "a serious internal problem while trying to communicate with OSS, "
					+ "such as not being able to access the network.");
			log.warn("Error Message: " + ce.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			/*
			 * Do not forget to shut down the client finally to release all
			 * allocated resources.
			 */
			client.shutdown();
		}
		return "";
	}
	public static String uploadUrl(MultipartFile file) throws Exception {

		client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		String ext = file.getOriginalFilename();
		ext = ext.substring(ext.lastIndexOf("."), ext.length());
		log.warn(ext);
		try {
			if (ext.isEmpty())
				throw new RuntimeException("文件类型未知");
			key = GenerateUtil.geneDate() + "/" + GenerateUtil.geneCode() + ext;

			Long firstPosition = 0L;
			InputStream instream = new ByteArrayInputStream(file.getBytes());
			AppendObjectResult appendObjectResult = client
					.appendObject(new AppendObjectRequest(bucketName, key,
							instream).withPosition(0L));
			// URL url = client.generatePresignedUrl(bucketName, key,
			// DateUtils.addYears(new Date(), 2));
			
			String url = "http://" + bucketName + "." + img_endpoint + "/"
					+ key;
		    System.out.println(bucketName);
		    System.out.println(img_endpoint);
//			if (!new SexSample().detectImages(url))// 鉴黄
				return url;
//			else
//				return "sexy";
		} catch (OSSException oe) {
			log.warn("Caught an OSSException, which means your request made it to OSS, "
					+ "but was rejected with an error response for some reason.");
			log.warn("Error Message: " + oe.getErrorCode());
			log.warn("Error Code:       " + oe.getErrorCode());
			log.warn("Request ID:      " + oe.getRequestId());
			log.warn("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			log.warn("Caught an ClientException, which means the client encountered "
					+ "a serious internal problem while trying to communicate with OSS, "
					+ "such as not being able to access the network.");
			log.warn("Error Message: " + ce.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			/*
			 * Do not forget to shut down the client finally to release all
			 * allocated resources.
			 */
			client.shutdown();
		}
		return "";
	}
	private static File createTempFile() throws IOException {

		File file = File.createTempFile("oss-java-sdk-", ".txt");
		file.deleteOnExit();

		return file;
	}

}
